//
//  ZMYUpdateNickNameViewController.m
//  奇阅
//
//  Created by 奇阅科技 on 14-10-29.
//  Copyright (c) 2014年 奇阅科技. All rights reserved.
//

#import "ZMYUpdateNickNameViewController.h"
#import "AppDelegate.h"
#import "Config.h"
@interface ZMYUpdateNickNameViewController ()
{
    NSString *nickName;
}
@end

@implementation ZMYUpdateNickNameViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setBarBackgroundImage:@"navBackground"];
    [self setTitleForNavigation:@"修改昵称"];
    [self setLeftBackButtonForNavigation];
    UserAccount *user = [UserAccount currentAccount];
    if (user.nickname.length>1) {
        nickName = user.nickname;
        self.textField.text = user.nickname;
    } else {
        nickName = user.nickname;
        self.textField.text = user.username;
    }
}
- (IBAction)update:(id)sender {
    [self.textField resignFirstResponder];
    if ([self.textField.text isEqualToString:nickName]) {
        [self.navigationController popViewControllerAnimated:YES];
        return;
    }else{
        if (self.textField.text.length<1) {
            [AppDelegate showHUDAndHide:@"昵称不能低于1个字符" view:self.view];
        } else if(self.textField.text.length>8){
            [AppDelegate showHUDAndHide:@"昵称不能多于8个字符" view:self.view];
        }else{
            [self.navigationController setSGProgressPercentage:60 andTintColor:YellowColor];
            [[ZMYNetWorkAPI default] updateNickName:self.textField.text response:^(NSDictionary *resultDic) {
                [self.navigationController setSGProgressPercentage:100];
                UserAccount *user = [UserAccount currentAccount];
                nickName  = self.textField.text;
                user.nickname =nickName;
                [UserAccount saveAccount:user];
                [self.navigationController popViewControllerAnimated:YES];
            } error:^(NSString *errorStr) {
                [self.navigationController cancelSGProgress];
                [AppDelegate showHUDAndHide:errorStr view:self.view];
            }];
        }
    }
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [self.textField resignFirstResponder];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
